#include "petmanage.h"
#include "ui_petmanage.h"
#include <QTreeWidget>
#include <QDebug>
#include <QTableWidget>
#include "adddoc.h"
#include "addguest.h"
#include "addpet.h"
//#include "readonlydelegate.h"

PetManage::PetManage(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::PetManage)
{
    ui->setupUi(this);

    sql = new SQLConn(this);
    sql->init();
    doctors = sql->getDocInfos();
    pets = sql->getPetInfos();
    guests = sql->getGueInfos();

    setTreeWidget();

    doctorInfo = new QTableWidget(this);
    guestInfo = new QTableWidget(this);
    petInfo = new QTableWidget(this);
    doctorMan = new QTableWidget(this);
    guestMan = new QTableWidget(this);
    petMan = new QTableWidget(this);

    setDocInfoTable();
    setGueInfoTable();
    setPetInfoTable();
    setDocManTable();
    setGueManTable();
    setPetManTable();

    ui->stackedWidget->addWidget(doctorInfo);
    ui->stackedWidget->addWidget(guestInfo);
    ui->stackedWidget->addWidget(petInfo);
    ui->stackedWidget->addWidget(doctorMan);
    ui->stackedWidget->addWidget(guestMan);
    ui->stackedWidget->addWidget(petMan);


}

PetManage::~PetManage()
{
    delete ui;
}

void PetManage::setDocInfoTable()
{
    doctorInfo->setColumnCount(9);
    doctorInfo->setRowCount(doctors.size());
    doctorInfo->setHorizontalHeaderLabels(QStringList()<<"医生编号"<<"医生姓名"<<"医生年龄"<<"医生性别"<<"工作年限"<<"工作领域"<<"是否出勤"<<"是否空闲"<<"联系方式");
    setInfosToInfoTable(doctorInfo, doctors);
}

void PetManage::setGueInfoTable()
{
    guestInfo->setColumnCount(6);
    guestInfo->setRowCount(guests.size());
    guestInfo->setHorizontalHeaderLabels(QStringList()<<"顾客编号"<<"顾客姓名"<<"顾客性别"<<"顾客年龄"<<"联系方式"<<"注册时间");

    setInfosToInfoTable(guestInfo, guests);
}

void PetManage::setPetInfoTable()
{
    petInfo->setColumnCount(9);
    petInfo->setRowCount(pets.size());
    petInfo->setHorizontalHeaderLabels(QStringList()<<"宠物编号"<<"宠物姓名"<<"宠物性别"<<"宠物年龄"<<"宠物品种"<<"主人编号"<<"主人姓名"<<"身体状况"<<"联系方式");

    setInfosToInfoTable(petInfo, pets);
}

void PetManage::setDocManTable()
{
    doctorMan->setColumnCount(10);
    doctorMan->setRowCount(doctors.size());
    doctorMan->setHorizontalHeaderLabels(QStringList()<<"医生编号"<<"医生姓名"<<"医生年龄"<<"医生性别"<<"工作年限"<<"工作领域"<<"是否出勤"<<"是否空闲"<<"联系方式"<<"");


    setInfosToManTable(doctorMan,doctors);
    connect(doctorMan, &QTableWidget::itemPressed, this, &PetManage::on_deleteDoc);
    connect(doctorMan, &QTableWidget::cellChanged, this, &PetManage::on_updateDoc);
}

void PetManage::setGueManTable()
{
    guestMan->setColumnCount(7);
    guestMan->setRowCount(guests.size());
    guestMan->setHorizontalHeaderLabels(QStringList()<<"顾客编号"<<"顾客姓名"<<"顾客性别"<<"顾客年龄"<<"联系方式"<<"注册时间"<<"");


    setInfosToManTable(guestMan,guests);
    connect(guestMan, &QTableWidget::itemPressed, this, &PetManage::on_deleteGue);
    connect(guestMan, &QTableWidget::cellChanged, this, &PetManage::on_updateGue);

}

void PetManage::setPetManTable()
{
    petMan->setColumnCount(10);
    petMan->setRowCount(pets.size());
    petMan->setHorizontalHeaderLabels(QStringList()<<"宠物编号"<<"宠物姓名"<<"宠物性别"<<"宠物年龄"<<"宠物品种"<<"主人编号"<<"主人姓名"<<"身体状况"<<"联系方式"<<"");


    setInfosToManTable(petMan,pets);
//    petMan->setItemDelegateForColumn(6, new ReadOnlyDelegate(this));//设置某列不可修改
//    petMan->setItemDelegateForColumn(8, new ReadOnlyDelegate(this));
    connect(petMan, &QTableWidget::itemPressed, this, &PetManage::on_deletePet);
    connect(petMan, &QTableWidget::cellChanged, this, &PetManage::on_updatePet);
}

void PetManage::setTreeWidget()
{
    ui->treeWidget->setHeaderLabels(QStringList()<<"宠物医院系统");
    ui->treeWidget->setHeaderHidden(true); //表头隐藏
    ui->treeWidget->expandAll();//展开所有节点
    QTreeWidgetItem *info = new QTreeWidgetItem(QStringList()<<"信息总览");
    ui->treeWidget->addTopLevelItem(info);

    QTreeWidgetItem *manage = new QTreeWidgetItem(QStringList()<<"系统管理");
    ui->treeWidget->addTopLevelItem(manage);

    QTreeWidgetItem *infochild1 = new QTreeWidgetItem(QStringList()<<"医生信息");
    QTreeWidgetItem *infochild2 = new QTreeWidgetItem(QStringList()<<"客户信息");
    QTreeWidgetItem *infochild3 = new QTreeWidgetItem(QStringList()<<"宠物信息");

    info->addChild(infochild1);
    info->addChild(infochild2);
    info->addChild(infochild3);

    QTreeWidgetItem *manachild1 = new QTreeWidgetItem(QStringList()<<"医生管理");
    QTreeWidgetItem *manachild2 = new QTreeWidgetItem(QStringList()<<"客户管理");
    QTreeWidgetItem *manachild3 = new QTreeWidgetItem(QStringList()<<"宠物管理");

    manage->addChild(manachild1);
    manage->addChild(manachild2);
    manage->addChild(manachild3);
}


void PetManage::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
{
    if (item->text(column) == "医生信息")
    {
        ui->stackedWidget->setCurrentIndex(2);
    }
    else if(item->text(column) == "客户信息")
    {
         ui->stackedWidget->setCurrentIndex(3);
    }
    else if(item->text(column) == "宠物信息")
    {
        ui->stackedWidget->setCurrentIndex(4);
    }
    else if(item->text(column) == "医生管理")
    {
        ui->stackedWidget->setCurrentIndex(5);
    }
    else if(item->text(column) == "客户管理")
    {
        ui->stackedWidget->setCurrentIndex(6);
    }
    else if(item->text(column) == "宠物管理")
    {
        ui->stackedWidget->setCurrentIndex(7);
    }
}

void PetManage::on_deleteDoc(QTableWidgetItem *item)
{
    int row = item->row();

    if (item->column()==9) {
         int id = doctorMan->item(row,0)->text().toInt();
         sql->delete_doctor(id);
         doctorMan->removeRow(row);
         doctors = sql->getDocInfos();
         doctorInfo->clear();
         setDocInfoTable();
    }
}

void PetManage::on_updateDoc(int row, int column)
{
    int id = doctorMan->item(row, 0)->text().toInt();
    QString newvalue = doctorMan->item(row, column)->text();
    sql->update_doctor(id, column, newvalue);
    doctors = sql->getDocInfos();
    doctorInfo->clear();
    setDocInfoTable();
}


void PetManage::on_deleteGue(QTableWidgetItem *item)
{
    int row = item->row();

    if (item->column()==6) {
         int id = guestMan->item(row,0)->text().toInt();
         sql->delete_guest(id);
         guestMan->removeRow(row);
         guests = sql->getGueInfos();
         guestInfo->clear();
         setGueInfoTable();
    }
}

void PetManage::on_updateGue(int row, int column)
{
    int id = guestMan->item(row, 0)->text().toInt();
    QString newvalue = guestMan->item(row, column)->text();
    sql->update_guest(id, column, newvalue);
    guests = sql->getGueInfos();
    guestInfo->clear();
    setGueInfoTable();
}


void PetManage::on_deletePet(QTableWidgetItem *item)
{
    int row = item->row();

    if (item->column()==9) {
         int id = petMan->item(row,0)->text().toInt();
         sql->delete_pet(id);
         petMan->removeRow(row);
         pets = sql->getPetInfos();
         petInfo->clear();
         setPetInfoTable();
    }
}

void PetManage::on_updatePet(int row, int column)
{
    int id = petMan->item(row, 0)->text().toInt();
    QString newvalue = petMan->item(row, column)->text();
    sql->update_pet(id, column, newvalue);
    pets = sql->getPetInfos();
    petInfo->clear();
    setPetInfoTable();
}

void PetManage::change_doc()
{
    doctors = sql->getDocInfos();
    doctorMan->blockSignals(true);
    doctorMan->clear();
    setDocManTable();
    doctorMan->blockSignals(true);
    doctorInfo->clear();
    setDocInfoTable();

}



void PetManage::on_add_gue_clicked()
{
    AddGuest addGueUI;
    addGueUI.setSQL(sql);
    addGueUI.exec();
    guests = sql->getGueInfos();
    guestMan->blockSignals(true);
    guestMan->clear();
    setGueManTable();
    guestMan->blockSignals(false);
    guestInfo->clear();
    setGueInfoTable();
}

void PetManage::on_add_doc_clicked()
{
    connect(sql, &SQLConn::sql_operator_success,this,&PetManage::change_doc);
    AddDoc addDocUi;
    addDocUi.setSQL(sql);
    addDocUi.exec();
}

void PetManage::on_add_pet_clicked()
{
    AddPet addPetUI;
    addPetUI.setSQL(sql);
    addPetUI.exec();
    pets = sql->getPetInfos();
    petMan->blockSignals(true);
    petMan->clear();
    setPetManTable();
    petMan->blockSignals(false);
    petInfo->clear();
    setPetInfoTable();
}
